package com.gitbl.webchat.utils;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.List;

public class RedisHelper {
    public static ShardedJedisPool newPool(String[] hosts){
        ShardedJedisPool redispool;
        JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
        config.setMaxTotal(50);//最大活动的对象个数
        config.setMaxIdle(1000 * 60);//对象最大空闲时间
        config.setMaxWaitMillis(1000 * 10);//获取对象时最大等待时间
        config.setTestOnBorrow(true);
        List jdsInfoList =new ArrayList(hosts.length);
        for(String h : hosts){
            String[] ipandport = h.split(":");
            int portA = 6379;
            if(ipandport.length>1){
                portA = Integer.parseInt(ipandport[1]);
            }
            JedisShardInfo infoA = new JedisShardInfo(ipandport[0], portA);

            jdsInfoList.add(infoA);
        }
        //jdsInfoList.add(infoB);
        redispool =new ShardedJedisPool(config, jdsInfoList);
        return redispool;
    }
}
